﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace HRMService
{
    public partial class MainService : IEmployeeShift
    {
        public DataSet EmployeeShift_GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet EmployeeShift_GetEmployee(  int DepartmentID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Employee_GetForShift", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
                      
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet EmployeeShift_GetList(  int DepartmentID, DateTime _date)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_GetList", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;                      
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);            
            dbCmd.Parameters.AddWithValue("@Date", _date);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
               
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet EmployeeShift_GetWithDepartment(int DepartmentID, int _month, int _year)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_GetWithDepartment", dbConn);
            DateTime FromDate = new DateTime(_year, _month, 1);
            DateTime ToDate = FromDate.AddMonths(1).AddDays(-1);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", DepartmentID);
            dbCmd.Parameters.AddWithValue("@FromDate", FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", ToDate);
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public EmployeeShiftInfo EmployeeShift_GetInfo(int ESID)
        {
            EmployeeShiftInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_GetInfo", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeShift_ID", ESID);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new EmployeeShiftInfo();
                    retVal.ID = Convert.ToInt32(dr["EmployeeShift_ID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["EmployeeShift_EmployeeID"]);
                    retVal.ShiftID = Convert.ToInt32(dr["EmployeeShift_ShiftID"]);
                    retVal.Date = Convert.ToDateTime(dr["EmployeeShift_Date"]);
                    retVal.TimeIn = Convert.ToDateTime(dr["EmployeeShift_TimeIn"]);
                    retVal.TimeOut = Convert.ToDateTime(dr["EmployeeShift_TimeOut"]);
                    retVal.DepartmentID = Convert.ToInt32(dr["EmployeeShift_DepartmentID"]);
                    retVal.TimeInB = dr["EmployeeShift_TimeInB"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EmployeeShift_TimeInB"]);
                    retVal.TimeOutB = dr["EmployeeShift_TimeOutB"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EmployeeShift_TimeOutB"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public EmployeeShiftInfo EmployeeShift_GetShift(int EmployeeID, DateTime _date)
        {
            EmployeeShiftInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_GetShift", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@Date", _date);
            SqlDataReader dr = null;
            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new EmployeeShiftInfo();
                    retVal.ShiftID = Convert.ToInt32(dr["EmployeeShift_ShiftID"]);
                    retVal.EmployeeID = Convert.ToInt32(dr["EmployeeShift_EmployeeID"]);
                    retVal.TimeIn = dr["EmployeeShift_TimeIn"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EmployeeShift_TimeIn"]);
                    retVal.TimeOut = dr["EmployeeShift_TimeOut"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EmployeeShift_TimeOut"]);
                    retVal.TimeInB = dr["EmployeeShift_TimeInB"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EmployeeShift_TimeInB"]);
                    retVal.TimeOutB = dr["EmployeeShift_TimeOutB"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(dr["EmployeeShift_TimeOutB"]);
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }

        public int EmployeeShift_Insert(EmployeeShiftInfo eInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeShift_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@EmployeeShift_ShiftID", eInfo.ShiftID);
            dbCmd.Parameters.AddWithValue("@EmployeeShift_DepartmentID", eInfo.DepartmentID);
            if (eInfo.Date != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_Date", eInfo.Date);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_Date", DBNull.Value);

            if (eInfo.TimeIn != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeIn", eInfo.TimeIn);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeIn", DBNull.Value);

            if (eInfo.TimeOut != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOut", eInfo.TimeOut);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOut", DBNull.Value);

            if (eInfo.TimeInB != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeInB", eInfo.TimeInB);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeInB", DBNull.Value);

            if (eInfo.TimeOutB != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOutB", eInfo.TimeOutB);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOutB", DBNull.Value);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool EmployeeShift_Update(EmployeeShiftInfo eInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;

            dbCmd.Parameters.AddWithValue("@EmployeeShift_EmployeeID", eInfo.EmployeeID);
            dbCmd.Parameters.AddWithValue("@EmployeeShift_ShiftID", eInfo.ShiftID);
            //dbCmd.Parameters.AddWithValue("@EmployeeShift_DepartmentID", eInfo.DepartmentID);
            if (eInfo.Date != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_Date", eInfo.Date);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_Date", DBNull.Value);

            if (eInfo.TimeIn != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeIn", eInfo.TimeIn);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeIn", DBNull.Value);

            if (eInfo.TimeOut != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOut", eInfo.TimeOut);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOut", DBNull.Value);

            if (eInfo.TimeInB != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeInB", eInfo.TimeInB);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeInB", DBNull.Value);

            if (eInfo.TimeOutB != DateTime.MinValue)
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOutB", eInfo.TimeOutB);
            else
                dbCmd.Parameters.AddWithValue("@EmployeeShift_TimeOutB", DBNull.Value);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }


        public bool EmployeeShift_Delete(int ESID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_Delete", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeShift_ID", ESID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool EmployeeShift_DeleteEmployee(int _departmentID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_DeleteEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeShift_DepartmentID", _departmentID);

            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool EmployeeShift_CheckExistEmployee(int EmployeeID, int EmployeeShift_ShiftID, DateTime date)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_CheckExistEmployee", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@EmployeeShift_EmployeeID", EmployeeID);
            dbCmd.Parameters.AddWithValue("@EmployeeShift_ShiftID", EmployeeShift_ShiftID);
            dbCmd.Parameters.AddWithValue("@EmployeeShift_Date", date);
            
            try
            {
                dbConn.Open();
                int Count = Convert.ToInt32(dbCmd.ExecuteScalar());
                retVal = Count > 0 ? true : false;

            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public bool EmployeeShift_InsertAutoShiftHC(int _DepartmentID, DateTime _FromDate, DateTime _ToDate)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_EmployeeShift_AutoInsertShiftHC", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@DepartmentID", _DepartmentID);
            dbCmd.Parameters.AddWithValue("@FromDate", _FromDate);
            dbCmd.Parameters.AddWithValue("@ToDate", _ToDate);
            try
            {
                dbConn.Open();
                dbCmd.ExecuteNonQuery();
            }
            catch
            {
                retVal = false;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}

